package com.chuanjiabao.wisdom.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.model.GeneratePresignedUrlRequest;
import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.FamilyTextCallbackBean;
import com.chuanjiabao.wisdom.databinding.ItemFamilyPeopleFileImageBinding;
import com.chuanjiabao.wisdom.listener.OnItemClickListener;
import com.chuanjiabao.wisdom.listener.OnPositionClickListener;
import com.chuanjiabao.wisdom.util.GlideUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.alibaba.sdk.android.oss.common.HttpMethod.GET;

/**
 * Created by 86178 on 2019/8/19.
 * author mogen
 * 用心，成就一切。
 */

public class FamilyPeopleFileImageAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<FamilyTextCallbackBean.DataBeanX.DataBean> strings;
    private Context mcontext;
    OnItemClickListener mOnItemClickLitener;
    OSSClient oss;
    List<CheckBox> lists = new ArrayList<>();

    public OnPositionClickListener listener;

    public void setOnPositionClickListener(OnPositionClickListener listener) {
        this.listener = listener;
    }

    public FamilyPeopleFileImageAdapter(Context context, List<FamilyTextCallbackBean.DataBeanX.DataBean> strings
            , OSSClient oss) {
        this.mcontext = context;
        this.strings = strings;
        this.oss = oss;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemFamilyPeopleFileImageBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
                R.layout.item_family_people_file_image, parent, false);
        binding.setAdapter(this);
        lists.add(binding.tvModuleSelect);
        return new TourViewHolder(binding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        final TourViewHolder tv = (TourViewHolder) holder;
        if (2 == strings.get(position).getFile_type()) {
            try {
                tv.ivModule.setVisibility(View.VISIBLE);
                tv.ivModulePlay.setVisibility(View.VISIBLE);

                String url = oss.presignConstrainedObjectURL(
                        strings.get(position).getFile_bucket(),
                        strings.get(position).getVideo_img(),
                        30 * 60
                );

                GlideUtil.setUrlImg(
                        mcontext,
                        url,
                        strings.get(position).getVideo_img(),
                        tv.ivModule
                );
            } catch (ClientException e) {
                e.printStackTrace();
            }
        } else if (1 == strings.get(position).getFile_type()) {
            try {
                tv.ivModule.setVisibility(View.VISIBLE);

                GeneratePresignedUrlRequest generatePresignedUrlRequest =
                        new GeneratePresignedUrlRequest(
                                strings.get(position).getFile_bucket(),
                                strings.get(position).getFile(),
                                30 * 60,
                                GET
                        );

                Map<String, String> map = new HashMap<>();
                map.put("x-oss-process", strings.get(position).getParams().getXossprocess());
                map.put("test", strings.get(position).getParams().getTest());

                generatePresignedUrlRequest.setQueryParameter(map);
                String url = oss.presignConstrainedObjectURL(generatePresignedUrlRequest);

                GlideUtil.setUrlImg(
                        mcontext,
                        url,
                        strings.get(position).getFile(),
                        tv.ivModule
                );
            } catch (ClientException e) {
                e.printStackTrace();
            }
        } else {
            tv.tvModuleText.setVisibility(View.VISIBLE);
            tv.tvModuleText.setText(strings.get(position).getContent());
        }

        tv.tvModuleSelect.setOnClickListener(v -> {
            if (listener != null) {
                listener.setOnPositionClickListener(position);
            }
        });

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(v -> {
                int pos = holder.getLayoutPosition();
                mOnItemClickLitener.onItemClick(holder.itemView, pos);
            });

            holder.itemView.setOnLongClickListener(v -> {
                int pos = holder.getLayoutPosition();
                mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                return false;
            });
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<FamilyTextCallbackBean.DataBeanX.DataBean> {
        ItemFamilyPeopleFileImageBinding functionBinding;
        ImageView ivModule, ivModulePlay;
        TextView tvModuleSelect, tvModuleText;

        public TourViewHolder(ItemFamilyPeopleFileImageBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
            ivModule = dataBinding.ivModule;
            tvModuleSelect = dataBinding.tvModuleSelect;
            ivModulePlay = dataBinding.ivModulePlay;
            tvModuleText = dataBinding.tvModuleText;
        }

        @Override
        public void bindData(FamilyTextCallbackBean.DataBeanX.DataBean data) {
            functionBinding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }

    public void setCheckTextSelect(int position) {
        lists.get(position).setChecked(true);
    }

    public void setCheckTextSelectNo(int position) {
        lists.get(position).setChecked(false);
    }

    //设置选中状态显示
    public void setTVShow() {
        for (TextView tv : lists) {
            tv.setVisibility(View.VISIBLE);
        }
    }

    //设置选中状态隐藏
    public void setTVGone() {
        for (TextView tv : lists) {
            tv.setVisibility(View.GONE);
        }
    }

}